//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	MetaFormContainer.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System.Collections.Generic;

namespace Smart.Common.Forms
{
	/// <summary>
	/// Form container control
	/// </summary>
	public class MetaFormContainer<TMetaCtrl> : MetaFormCtrl, IMetaFormContainer
		where TMetaCtrl : MetaFormCtrl
	{
		/// <summary>
		/// Child controls
		/// </summary>
		public List<TMetaCtrl> Items { get; protected set; }

		/// <summary>
		/// Form container control
		/// </summary>
		public MetaFormContainer(string title, string name)
			: base(title, name)
		{
			Items = new List<TMetaCtrl>();
		}

		/// <summary>
		/// Set all children Parent field
		/// </summary>
		public virtual void SetParent()
		{
			foreach (var ctrl in Items)
				ctrl.Parent = this;
		}

		/// <summary>
		/// Fill all child controls visibility
		/// </summary>
		public override void EnsureVisible(bool visible)
		{
			base.EnsureVisible(visible);

			foreach (var ctrl in Items)
				ctrl.EnsureVisible(visible);
		}

		public virtual MetaFormCtrl FindByName(string name)
		{
			if (Items == null || Items.Count <= 0)
				return null;

			foreach (var item in Items)
			{
				if (item.Name.EqIC(name))
					return item;

				var container = item as IMetaFormContainer;

				if (container == null)
					continue;

				var found = (container).FindByName(name);

				if (found != null)
					return found;
			}

			return null;
		}
	}
}